﻿Imports System.Data
Imports NVelocity
Imports NVelocity.App
Imports NVelocity.Context
Imports System.Collections.Generic
Imports System.Xml
Partial Class Public_ExportCRMS
    Inherits System.Web.UI.Page

    Protected Sub btnExportWord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportWord.Click
        Me.Export("Word")
    End Sub

    Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
        Me.Export("Excel")
    End Sub

    Protected Sub Export(Byval ExportType as string)
        Dim dat As New clsData
        Dim sql As New StringBuilder
        Dim dt As DataTable

        sql.AppendFormat("select FormID from CRMS.dbo.crms_CaseReportForm order by FormID")

        dt = dat.GetData(sql.ToString())

        Dim vList As New List(Of MyCrmsList)
        Dim v As MyCrmsList

        Dim i As Integer
        For i = 0 To dt.Rows.Count - 1 Step 6

            v = New MyCrmsList

            v.FormID1 = dt.Rows(i)("FormID").ToString()
            If i + 1 <= dt.Rows.Count - 1 Then
                v.FormID2 = dt.Rows(i + 1)("FormID").ToString()
            Else
                v.FormID2 = ""
            End If
            If i + 2 <= dt.Rows.Count - 1 Then
                v.FormID3 = dt.Rows(i + 2)("FormID").ToString()
            Else
                v.FormID3 = ""
            End If
            If i + 3 <= dt.Rows.Count - 1 Then
                v.FormID4 = dt.Rows(i + 3)("FormID").ToString()
            Else
                v.FormID4 = ""
            End If
            If i + 4 <= dt.Rows.Count - 1 Then
                v.FormID5 = dt.Rows(i + 4)("FormID").ToString()
            Else
                v.FormID5 = ""
            End If
            If i + 5 <= dt.Rows.Count - 1 Then
                v.FormID6 = dt.Rows(i + 5)("FormID").ToString()
            Else
                v.FormID6 = ""
            End If
            vList.Add(v)
        Next


        'Print
        Dim fileName As String
        If ExportType = "Excel" Then
            fileName = "ExportCrms.xls"
        Else
            fileName = "ExportCrms.doc"
        End If
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName)
        Response.ContentType = "application/msword"

        Dim velocity As New VelocityEngine
        Dim props As New Commons.Collections.ExtendedProperties

        'fix bug
        props.AddProperty("file.resource.loader.path", Server.MapPath("~\App_Data"))
        props.AddProperty("input.encoding", "utf-8")
        props.AddProperty("output.encoding", "utf-8")
        velocity.Init(props)

        Dim template As Template

        Dim context As New VelocityContext

        If ExportType = "Excel" Then
            template = velocity.GetTemplate("CRMSExcel.xml")
        Else
            template = velocity.GetTemplate("CRMSWord.xml")
        End If

        Dim RowCount As Integer = 0
        If dt.Rows.Count Mod 6 = 0 Then
            RowCount = dt.Rows.Count / 6
        Else
            RowCount = dt.Rows.Count / 6 + 1
        End If

        context.Put("RowCount", RowCount)
        context.Put("MyList", vList)

        Dim writer As New IO.StringWriter
        template.Merge(context, writer)

        Response.Write(writer.GetStringBuilder().ToString())

        Response.End()

    End Sub

End Class

Public Class MyCrmsList
    Private FormID1Field As String
    Private FormID2Field As String
    Private FormID3Field As String
    Private FormID4Field As String
    Private FormID5Field As String
    Private FormID6Field As String

    Public Property FormID1() As String
        Get
            Return Me.FormID1Field
        End Get
        Set(ByVal value As String)
            Me.FormID1Field = value
        End Set
    End Property

    Public Property FormID2() As String
        Get
            Return Me.FormID2Field
        End Get
        Set(ByVal value As String)
            Me.FormID2Field = value
        End Set
    End Property

    Public Property FormID3() As String
        Get
            Return Me.FormID3Field
        End Get
        Set(ByVal value As String)
            Me.FormID3Field = value
        End Set
    End Property

    Public Property FormID4() As String
        Get
            Return Me.FormID4Field
        End Get
        Set(ByVal value As String)
            Me.FormID4Field = value
        End Set
    End Property

    Public Property FormID5() As String
        Get
            Return Me.FormID5Field
        End Get
        Set(ByVal value As String)
            Me.FormID5Field = value
        End Set
    End Property

    Public Property FormID6() As String
        Get
            Return Me.FormID6Field
        End Get
        Set(ByVal value As String)
            Me.FormID6Field = value
        End Set
    End Property
End Class
